<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Channels</title>
<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../boost_asio.html" title="Boost.Asio">
<link rel="up" href="../overview.html" title="Overview">
<link rel="prev" href="signals.html" title="Signal Handling">
<link rel="next" href="posix.html" title="POSIX-Specific Functionality">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td>
<td align="center"><a href="../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="signals.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="posix.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_asio.overview.channels"></a><a class="link" href="channels.html" title="Channels">Channels</a>
</h3></div></div></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          This is an experimental feature.
        </p></td></tr>
</table></div>
<p>
        The templates <a class="link" href="../reference/experimental__basic_channel.html" title="experimental::basic_channel">experimental::basic_channel</a>
        and <a class="link" href="../reference/experimental__basic_concurrent_channel.html" title="experimental::basic_concurrent_channel">experimental::basic_concurrent_channel</a>,
        with aliases <code class="computeroutput"><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">channel</span></code>
        and <code class="computeroutput"><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">concurrent_channel</span></code>, may be used to send
        messages between different parts of the same application. A <span class="emphasis"><em>message</em></span>
        is defined as a collection of arguments to be passed to a completion handler,
        and the set of messages supported by a channel is specified by its template
        parameters. Messages may be sent and received using asynchronous or non-blocking
        synchronous operations.
      </p>
<p>
        For example:
      </p>
<pre class="programlisting"><span class="comment">// Create a channel with no buffer space.</span>
<span class="identifier">channel</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">(</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">size_t</span><span class="special">)&gt;</span> <span class="identifier">ch</span><span class="special">(</span><span class="identifier">ctx</span><span class="special">);</span>

<span class="comment">// The call to try_send fails as there is no buffer</span>
<span class="comment">// space and no waiting receive operations.</span>
<span class="keyword">bool</span> <span class="identifier">ok</span> <span class="special">=</span> <span class="identifier">ch</span><span class="special">.</span><span class="identifier">try_send</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">error</span><span class="special">::</span><span class="identifier">eof</span><span class="special">,</span> <span class="number">123</span><span class="special">);</span>
<span class="identifier">assert</span><span class="special">(!</span><span class="identifier">ok</span><span class="special">);</span>

<span class="comment">// The async_send operation is outstanding until</span>
<span class="comment">// a receive operation consumes the message.</span>
<span class="identifier">ch</span><span class="special">.</span><span class="identifier">async_send</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">error</span><span class="special">::</span><span class="identifier">eof</span><span class="special">,</span> <span class="number">123</span><span class="special">,</span>
    <span class="special">[](</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">)</span>
    <span class="special">{</span>
      <span class="comment">// ...</span>
    <span class="special">});</span>

<span class="comment">// The async_receive consumes the message. Both the</span>
<span class="comment">// async_send and async_receive operations complete</span>
<span class="comment">// immediately.</span>
<span class="identifier">ch</span><span class="special">.</span><span class="identifier">async_receive</span><span class="special">(</span>
    <span class="special">[](</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">size_t</span> <span class="identifier">n</span><span class="special">)</span>
    <span class="special">{</span>
      <span class="comment">// ...</span>
    <span class="special">});</span>
</pre>
<h5>
<a name="boost_asio.overview.channels.h0"></a>
        <span class="phrase"><a name="boost_asio.overview.channels.see_also"></a></span><a class="link" href="channels.html#boost_asio.overview.channels.see_also">See
        Also</a>
      </h5>
<p>
        <a class="link" href="../reference/experimental__basic_channel.html" title="experimental::basic_channel">experimental::basic_channel</a>,
        <a class="link" href="../reference/experimental__basic_concurrent_channel.html" title="experimental::basic_concurrent_channel">experimental::basic_concurrent_channel</a>,
        <a class="link" href="../examples/cpp20_examples.html#boost_asio.examples.cpp20_examples.channels">Channels examples
        (C++20)</a>.
      </p>
</div>
<div class="copyright-footer">Copyright © 2003-2024 Christopher M. Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="signals.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="posix.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
